#region File Description
//  XAEnima 
//  License: GNU/GPL
//  Developer: Federico Leonardo Motta
//  File description: This component display some initial images.
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XAEnima.Core.GameManager;
#endregion

namespace XAEnima.Core.Screens
{
    public delegate void IntroAction();
    public class Intro : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public event IntroAction OnFinish;
        SpriteBatch spriteBatch;
        Texture2D tex;
        float screenscale;
        Vector2 pos_tex = Vector2.Zero;
        private InputManager Input_Manager;
        String[] screens = { "Intro Images/sfondo1"};
        int screen_index=0;
        double counter=0;

        public Intro(Game game)
            : base(game)
        {
            //InputManager im = new InputManager(game);
            IInputManager InputManagerService = (IInputManager) Game.Services.GetService(typeof(IInputManager));
            InputManagerService.EnterPressed += new PressedEventHandler(EnterIsPressed);
            InputManagerService.EscapePressed += new PressedEventHandler(EnterIsPressed);
            InputManagerService.TabPressed += new PressedEventHandler(EnterIsPressed);
        }

        private void EnterIsPressed()
        {
            counter = 2200;
            //if (screen_index < 2)
            //{
            //    screen_index++; 
            //    tex = Game.Content.Load<Texture2D>(@screens[screen_index]);
            //}
            
        }

        public override void Initialize()
        {
            spriteBatch = (Game.Services.GetService(typeof(SharedContext)) as SharedContext).sprite_batch;
            pos_tex = new Vector2(0,0);
            tex = Game.Content.Load<Texture2D>(@screens[0]);
            screenscale = (float)Game.GraphicsDevice.Viewport.Width / 1600f;
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            counter += gameTime.ElapsedGameTime.TotalMilliseconds;
            
            if (counter >= 2200)
            { 
                //EVENTO USCITA
                if (OnFinish != null) OnFinish();
            }

            base.Update(gameTime);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Content.Unload();
            Game.Services.RemoveService(typeof(Intro));
            base.Dispose(disposing);
        }

        public void Detach()
        {
            // Detach the event and delete the list
            Input_Manager.EnterPressed -= new PressedEventHandler(EnterIsPressed);
            Input_Manager = null;
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(tex, pos_tex, null, Color.White, 0f, Vector2.Zero, screenscale, SpriteEffects.None, 0f);
            //spriteBatch.DrawString(Game.Content.Load<SpriteFont>("large_font"), "Quit", new Vector2(50, 290), Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
